<?php

namespace App\Repositories\Recharge;

class Jinmi
{
    static private $MerNo = "fcsw01";
    static private $key = "oJmTvsNNBvPn";

    /**
     * 网银支付
     * @param $data
     * @return string
     */
    static public function onlineBanking($data)
    {
        $info = [
            "Amount" => $data["money"] * 100,
            "Amount" => 1,
            "BankCode" => $data["bankId"],
            "BillNo" => $data["billNumber"],
            "GoodsSubject" => $data["goodsSubject"],
            "MerNo" => self::$MerNo,
            "NotifyURL" => $data["notifyUrl"],
            "Remark" => "remark",
            "ReturnURL" => $data["returnUrl"],
            "Service" => "pay.b2c"
        ];
        ksort($info);
        $encryptStr = md5(urldecode(http_build_query($info) . self::$key));
        $html = "
            <form id='form' action='http://www.laileda.com/api/payment.aspx' method='post' target='_top'>
		    <input type='hidden' name='Service' value={$info['Service']} />
		    <input type='hidden' name=MerNo value={$info['MerNo']} />
		    <input type='hidden' name=BillNo value={$info['BillNo']} />
		    <input type='hidden' name=Amount value={$info['Amount']} />
		    <input type='hidden' name=ReturnURL value={$info['ReturnURL']} />
		    <input type='hidden' name=NotifyURL value={$info['NotifyURL']} />
		    <input type='hidden' name=MD5info value={$encryptStr} />
		    <input type='hidden' name=GoodsSubject value={$info['GoodsSubject']} />
		    <input type='hidden' name=BankCode value={$info['BankCode']} />
		    <input type='hidden' name=Remark value={$info['Remark']} />
	    </form>
	    <script>
		    document.getElementById('form').submit();
	    </script>
        ";
        return $html;
    }

    /**
     * 快捷支付
     * @param $data
     * @return string
     */
    static public function quickPayment($data)
    {
        $info = [
            "Amount" => $data["money"] * 100,
            "Amount" => 1,
            "BillNo" => $data["billNumber"],
            "GoodsSubject" => $data["goodsSubject"],
            "MerNo" => self::$MerNo,
            "NotifyURL" => $data["notifyUrl"],
            "Remark" => "remark",
            "ReturnURL" => $data["returnUrl"],
            "Service" => "pay.kj",
            "UserId" => $data["userId"],
        ];
        ksort($info);
        $encryptStr = md5(urldecode(http_build_query($info) . self::$key));
        $html = "
            <form id='form' action='http://www.laileda.com/api/payment.aspx' method='post' target='_top'>
		    <input type='hidden' name='Service' value={$info['Service']} />
		    <input type='hidden' name=MerNo value={$info['MerNo']} />
		    <input type='hidden' name=BillNo value={$info['BillNo']} />
		    <input type='hidden' name=Amount value={$info['Amount']} />
		    <input type='hidden' name=ReturnURL value={$info['ReturnURL']} />
		    <input type='hidden' name=NotifyURL value={$info['NotifyURL']} />
		    <input type='hidden' name=MD5info value={$encryptStr} />
		    <input type='hidden' name=GoodsSubject value={$info['GoodsSubject']} />
		    <input type='hidden' name=UserId value={$info['UserId']} />
		    <input type='hidden' name=Remark value={$info['Remark']} />
	    </form>
	    <script>
		    document.getElementById('form').submit();
	    </script>
        ";
        return $html;
    }

    /**
     * 验证回调合法性
     * @param $data
     */
    static public function checkNotify()
    {
        $data = request()->all();
        ksort($data);
        $md5 = $data["MD5info"];
        unset($data["MD5info"]);

        return $md5 == md5(urldecode(http_build_query($data)) . "oJmTvsNNBvPn");
    }

    /**
     * 支付是否成功
     * @param $status
     * @return bool
     */
    static public function isPaySuccess($status)
    {
        return $status == 1;
    }
}